const app = getApp()
import { getSampleDetail, postDeleteOrder, postRemindDelivery, postConfirmReceipt } from '../../../../api/index'
import { formatTimeTwo } from '../../../../utils/util'

Page({
  data: {
    orderId: '',
    detail: {}
  },
  async delOrder () {
    try {
      if (this.locking) return
      const { confirm } = await wx.$pro.showModal({ title: '确定要删除该订单吗？' })
      this.locking = true
      if (confirm) {
        await postDeleteOrder({ orderId: this.data.detail.id })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        wx.$nav.navigateBack()
      }
      this.locking = false
    } catch (err) {
      this.locking = false
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async remindDelivery () {
    try {
      await postRemindDelivery({ orderId: this.data.detail.id })
      await wx.$pro.showModal({ title: '已提醒商家发货' })
      this.getDetail()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async confirmDelivery () {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '确认收到货了吗？', content: '为保证你的售后权益，请收到商品确认无误后再确认收货' })
      if (confirm) {
        await postConfirmReceipt({ orderId: this.data.detail.id })
        wx.showToast({ title: '已确认收货！', icon: 'none' })
        this.getDetail()
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getDetail () {
    try {
      const { ResultSet } = await getSampleDetail({ orderId: this.data.orderId })
      ResultSet.userData.detail.orderDate = formatTimeTwo(ResultSet.userData.detail.orderDate, 'Y-M-D h:m:s')
      ResultSet.userData.detail.deliveryDate = formatTimeTwo(ResultSet.userData.detail.deliveryDate, 'Y-M-D h:m:s')
      ResultSet.userData.detail.selfDeliveryDate = formatTimeTwo(ResultSet.userData.detail.selfDeliveryDate, 'Y-M-D h:m:s')
      this.setData({ detail: ResultSet.userData.detail })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad ({ id }) {
    this.setData({ orderId: id })
    this.getDetail()
  }
})